#include <cstdint>
#include <iostream>
#include <istream>
#include <string>
#include <utility>
#include <vector>
using ll = int64_t;

const ll maxlen = 1e3;

#define s(j)s[(j)]-'a'
#define pv(v)do{/*std::cout<<#v<<" :"<<(v)<<'\n';*/}while(0)

static inline void solve(){
    ll n,q;
    std::cin>>n>>q;
    static std::vector<std::vector<std::pair<ll, ll>>> t;
    t.clear();
    t.resize(maxlen,std::vector<std::pair<ll, ll>>(27));
    ll tot=1;
    pv("input");
    for(ll i=1;i<=n;i++){
        static std::string s;
        std::cin>>s;
        ll now=0;
        for(ll j=0;j<s.size();j++){
            pv(j);
            pv(now);
            pv(s(j));
            t[now][s(j)].first++;
            pv(t[now][s(j)].first);
            if(t[now][s(j)].second==0){
                t[now][s(j)].second=tot++;
            }
            now=t[now][s(j)].second;
        }
    }
    for(ll i=1;i<=q;i++){
        ll ans=0;
        static std::string s;
        std::cin>>s;
        ll now=0;
        pv("check");
        for(ll j=0;j<s.size();j++){
            pv(j);
            pv(now);
            pv(t[now][s(j)].first);
            if(t[now][s(j)].second==0){
                break;
            }else{
                ans+=t[now][s(j)].first;
                now=t[now][s(j)].second;
            }
        }
        std::cout<<ans<<'\n';
    }
    
}

int main(){
    std::iostream::sync_with_stdio(false);
    std::cin.tie(nullptr);
    ll T;
    std::cin>>T;
    while(T--){
        solve();
    }
}